﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WFNetLib;
using CMOSTestLib.SaperaGUI;

namespace CMOSTestLib
{
    public class Calc1
    {
        public static string TempPicPath_Light = System.Windows.Forms.Application.StartupPath + "\\Temp1L\\";
        public static string TempPicPath_Dark = System.Windows.Forms.Application.StartupPath + "\\Temp1D\\";
        public static double percent_base;//饱和点在70%±5%定义为曝光适度
        public static double percent;
        public static int k1;//欠曝光校正系数,范围约为500-1800,值变大，饱和点向左移动
        public static int k2;//过曝光校正系数,范围约为30-200,值变大，饱和点向右移动

        public static int p1;//饱和点最小位置
        public static int p2;  //饱和点最大位置
        public static int saturation = 0;  // %饱和点的图片编号       


        public static List<double> miu_y;
        public static List<double> miu_y_dark;
        public static List<double> delta_y;
        public static List<double> delta_y_dark;
        public static List<double> SNR;
        public static double PhotoelectricResponseCurve_k;//光电响应线性方程
        public static double PhotoelectricResponseCurve_b;
        public static double OverAllGain_K;//传感器总体增益
        public static double AverageDarkSignal_k;//平均暗信号线性方程
        public static double AverageDarkSignal_b;
        public static int SaturatedIndex;//饱和点位置索引
        public static int Saturated50Index;//50%饱和点位置索引
        public static List<double> miu_d;//平均暗信号
        public static double DR;//动态范围DR
        public static void TestExposureTime(byte[] ya,byte[] yb,int row,int col,out double y,out double d)
        {
            ushort[,] picA=wfSapGUI.TransPicDatas(ya,row,col);
            ushort[,] picB = wfSapGUI.TransPicDatas(yb, row, col);
            ulong y1 = 0;
            ulong d1 = 0;
            int x;
            for (int m = 0; m < row; m++)
            {
                for (int n = 0; n < col; n++)
                {
                    y1 += (ulong)picA[m,n] + picB[m,n];
                    x=picA[m,n]-picB[m,n];
                    x=x*x;
                    d1+=(ulong)x;
                }
            }
            y =(double)y1 / 2 / row / col;
            d = (double)d1 / 2 / row /col;
        }
        public static void TestExposureTime1(byte[] ya, int row, int col, out double y, out double d)
        {
            ushort[,] picA = wfSapGUI.TransPicDatas(ya, row, col);
            ulong y1 = 0;
            ulong d1 = 0;
            double x;
            for (int m = 0; m < row; m++)
            {
                for (int n = 0; n < col; n++)
                {
                    y1 += (ulong)picA[m, n];
                    //x = picA[m, n] - picB[m, n];
                    //x = x * x;
                    //d1 += (ulong)x;
                }
            }
            y = (double)y1 / row / col;
            for (int m = 0; m < row; m++)
            {
                for (int n = 0; n < col; n++)
                {
                    x = picA[m, n] - y;
                    x = x * x;
                    d1 += (ulong)x;
                }
            }
            
            d = (double)d1  / row / col;
        }
        public static bool CheckSaturatedIndex()
        {
            int flag = 0;//表示曝光情况，1-欠曝光，2-曝光适中，3-过度曝光
            if((saturation == 0) ||(saturation > p2 ))
                flag = 1;
            else if( ( saturation >= p1 )&&( saturation <= p2 ) )
                   flag = 2;
            else
                   flag = 3;

//             %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
//             %修正曝光步长，步长h、明场均值u、饱和点编号saturation，输出修正步长h_correct
//             %h_correct表示修正后的曝光步长
//             %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
            switch (flag)
            {
                case 1:
                    SystemParam.eStep = (uint)(SystemParam.eStep * (k1 - miu_y[0]) / (miu_y[(int)(SystemParam.ExposureTest_Ns* percent_base/100)] - miu_y[0]));
                    break;
                case 2:
                    return true;
                case 3:
                    SystemParam.eStep = (uint)(SystemParam.eStep * saturation/k2);
                    break;
            }
            return false;
        }
    }
}
